package com.easy.controller;

import com.easy.bean.Department;
import com.easy.service.DepartmentService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/api/department")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    // 新增部门
    @PostMapping
    public Result add(@RequestBody Department department) {
        return Result.success(departmentService.add(department));
    }

    // 编辑部门
    @PutMapping("/{id}")
    public Result edit(@PathVariable("id") Integer id, @RequestBody Department department) {
        department.setId(id);
        return Result.success(departmentService.edit(department));
    }

    // 删除部门
    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id) {
        return Result.success(departmentService.del(id));
    }

    // 查询所有部门（可模糊查询）
    @GetMapping
    public Result all(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "description", required = false) String description
    ) {
        return Result.success(departmentService.all(name, description));
    }



    // 查询单个部门
    @GetMapping("/{id}")
    public Result one(@PathVariable("id") Integer id) {
        return Result.success(departmentService.one(id));
    }

    @GetMapping("page")
    public Result page(Page page, Department  department){
        //查询一共有多少条数据
        //查询当前页要显示的数据
        //将以上数据封装到page对象中
        departmentService.page(page, department);
        //返回page对象
        return Result.success(page);
    }
}
